#include <stdio.h>
#include <string.h>

void mystrcat(char s1[], const char s2[])
{
    // s1: abc
    int len = strlen(s1);
    int i, j;
    
    for(i=len, j=0; s2[j]!='\0'; i++, j++)
        s1[i] = s2[j];

    s1[i] = s2[j];
}

void mystrncat(char s1[], const char s2[], int n/*最多可以拷贝的字符个数*/)
{
    // s1: abc
    int len = strlen(s1);
    int i, j;
    
    for(i=len, j=0; s2[j]!='\0' && j<n; i++, j++)
        s1[i] = s2[j];

    s1[i] = '\0';
}

int main(int argc, char const *argv[])
{
    char s1[10] = "abcdefgh";
    char s2[10] = "wxyz";

    // mystrcat(s1, s2);  // 等价于strcat(), 将s2拼接到s1的末尾，保证s2不变
    mystrncat(s1, s2, sizeof(s1)-strlen(s1)-1); // 等价于strncat(), 将s2拼接到s1的末尾，保证s2不变

    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);

    return 0;
}
